<?php
// 首页显示信息以及基本配置
namespace NewAdmin\Controller;
use Think\Controller;
class OfferListController extends CommonController {
    function _initialize() {
        parent::_initialize();
       
    }

    function index(){
       $offers = D('Offers');
        $users= D('Users');
        $where = 'ishidden = 0';
        $orderby = I('get.orderBy');
        if($orderby == "1"){
            $where3 = 'id ASC';
        }else if($orderby == "2"){
            $where3 = 'tmoney DESC';
        }else if($orderby == "3"){
            $where3 = 'tmoney ASC';
        }else{
            $where3 = 'id DESC';
        }
        $typeid = I('get.offers_type');
        //$userArray = $this->userArray;
        $where .= I('get.nickname') ? ' AND name like  "%' . I('get.nickname') . '%"' : '';
        $where .= I('get.address') ? ' AND address like  "%' . I('get.address') . '%"' : '';
        $where .=I('get.phone') ? ' AND phone like  "%' . I('get.phone') . '%"' : '';
        if (isset($typeid) && !($typeid == "" || $typeid == "0"))
        {
            $where .= $typeid ? ' AND offers_type =' . $typeid : '';
        }
        $hasCheck = I('get.hasCheck');
        if($hasCheck == "1"){
            $where .= $hasCheck?' AND hasCheck =0':'';
        }else if($hasCheck == "2"){
            $where .= $hasCheck?' AND hasCheck =1':'';
        }else if($hasCheck == "3"){
            $where .= $hasCheck?' AND hasCheck =2':'';
        }
        //$where .= I('get.type') ? ' AND type = "' . I('get.type') . '"' : '';
        $count = $offers->where($where)->count();
        $this->assign('offerCount',$count);
        $Page = new \Think\Page($count, 25); // 实例化分页类 传入总记录数
        $show = $Page->show1(); // 分页显示输出
        $arrayFlag = $offers->where($where)
            ->order($where3)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach ($arrayFlag as $key => $value) {
            $rs=$users->find($value['busers_id']);
            $arrayFlag[$key]['busers_phone']=$rs['phone'];
        }
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('arrayFlag', $arrayFlag);
        $this->assign('title', '本地商家列表');
        $this->display();
    }


     /**
     * 添加
     */
    function offerAdd()
    {
        if ($_GET['id']) {
            $this->assign('title', '编辑本地商家');
        } else {
            $this->assign('title', '添加本地商家');
        }
        //$userArray = $this->userArray;
        $offers = D('offers');
        $offersSale = D('offersSale');
        ////$offerType = D('OfferType');

        ////$offerTypeList = $offerType->where('ishidden = 0 AND bid != 0')->select();
        ////$this->assign('offerTypeList',$offerTypeList);

        $ID = I('get.id', 0);
        if ($ID) {
            // 当前商家信息
            $priceArray = $offers->where(' id = ' . $ID . '  AND ishidden = 0')->find();
            if ($priceArray) {
                // 当前折扣信息
                $offersSaleArray = $offersSale->where(' ishidden=0 AND  offerid = ' . $priceArray['id'] . '  AND bid=0')->select();
                // var_dump($offersSaleArray);
                // exit;
                foreach ($offersSaleArray as $key => $row) {
                    $offersSaleArray[$key]['sub'] = $offersSale->where(' ishidden=0 AND bid = ' . $row['id'] . ' ')->select();
                }
                $this->assign('offersSaleArray', $offersSaleArray);


                //根据分类id获取一级二级分类数组
                $offerType = D('OfferType');
                $offerTypeTemp = $offerType->where(' id='.$priceArray['type'].' AND ishidden = 0')->find();
                if ($offerTypeTemp)
                {
                    $priceArray['type_ids'] =  ",".$offerTypeTemp['bid'].",".$priceArray['type'].",";
                }
                else
                {
                    $priceArray['type_ids'] =",0,0,";
                }
            }
            $users = D('users');
            $offerMsg = $users->where('ishidden=0 AND id='.$priceArray['offerMsg'])->getField('username');
            $this->assign('offerMsg', $offerMsg);
            //   var_dump($priceArray);

            $this->assign('priceArray', $priceArray);
        } else {
            //初始化一级二级分类数组
            $priceArray['type_ids'] =",0,0,";

            $npassword = rand(100000, 999999);
            $this->assign('npassword', $npassword);
        }
        // 读取经销商日志
        $log = D('offers_log');
        $logArray = M()->field('log.*,user.nickname')
            ->table('__USER__ AS user')
            ->join('LEFT JOIN __OFFERS_LOG__ AS log ON (log.user_id = user.id)')
            ->where('workID = ' . $ID)
            ->order(' log.id DESC')
            ->select();
        $this->assign('logArray', $logArray);

        // 读取折扣修改日志

        $log1 = D('offersale_log');
        $logArray1 = M()->field('log.*,user.nickname')
            ->table('__USER__ AS user')
            ->join('LEFT JOIN __OFFERSALE_LOG__ AS log ON (log.user_id = user.id)')
            ->join('__OFFERS_SALE__ AS offersale ON offersale.id = log.workID')
            ->join('LEFT JOIN __OFFERS__ AS offers ON (offers.id = offersale.offerid)')
            ->where('offers.id = ' . $ID)
            ->order(' log.id DESC')
            ->select();
        $this->assign('logArray1', $logArray1);
        $this->display();
    }
   
}